<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Aitifen\Library\Xxtea\Xxtea;
use Illuminate\Http\Request;

class BannerController extends Controller
{
    /**
     * 焦点图列表
     */
    public function BannerList( Request $request )
    {
        try{

            $page    = isset($_GET['page'])    ?  (int)$_GET['page'] :1;
            $pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] :10;
            $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
            $result = $server->getBannerList($page,$pageSize);

        }catch (\Exception $e){

            return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);
        }

        return $result;

    }

    /**
     * @param Request $request
     * 编辑推荐图
     */
    public function updateBanner ( Request $request )
    {

        try{

            $request_data = $request->all();
            $banner_id = $request_data['id'];
            unset($request_data['id']);
            throw_unless($this->__validateBanner($request_data, $message, $code), new \Exception($message, $code));
            $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
            $condition = [
                'id'=>$banner_id,
                'status'=>0
            ];
            $result = $server->editBanner($condition,$request_data);

        }catch (\Exception $e){

            return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);
        }
        return response()->json(['message'=>'修改数据成功!','code'=>10000,$result]);
    }
    /**
     * @param Request $request
     * 添加推荐图
     */
      public function savaBanner( Request $request )
      {
          try {

              $request_data = $request->all();

              throw_unless($this->__validateBanner($request_data, $message, $code), new \Exception($message, $code));
              $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
              $result = $server->storeBanner($request_data);

          }catch (\Exception $e){

             return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);
          }
          return response()->json(['message'=>'添加数据成功!','code'=>10000,$result]);
      }
      public function delBanner( Request $request )
      {
          try{

              $request_data = $request->all();
              $banner_id  = $request_data['id'] ?? 0;
              $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
              $where = ['id'=>$banner_id];

              $result = $server->delBanner(['status'=>1],$where);

          }catch (\Exception $e){

              return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);

          }
          return response()->json(['message'=>'删除成功!','code'=>10000,$result]);

      }
    /**
     * @param $data
     * @param array $message
     * @param array $code
     * @return bool
     * 校验焦点图
     */
      protected function __validateBanner ($data,&$message=[], &$code=[] ):bool
      {

           if (empty($data['agency_id']))
           {
               throw new \Exception('机构id不能为空!', 10001 );
               return false;
            }

            if(!is_numeric($data['agency_id']))
            {
                throw new \Exception('机构id必须是数字类型!', 10001 );
                return false;
            }
            if (empty($data['url']))
            {
                throw new \Exception('图片不能为空!', 10001 );
                return false;
            }
          if (empty($data['address_url']))
          {
              throw new \Exception('链接地址不能为空!', 10001 );
              return false;
          }
          if(FALSE == $this->checkUrl($data['address_url']))
          {
                throw new \Exception('链接地址不合法!', 10001 );
                return false;
          }

           return true;
      }

    /**
     * @param $str
     * @return bool
     * 校验url
     */
     protected  function checkUrl($str) :bool
     {
        $isUrl = filter_var($str, FILTER_VALIDATE_URL);
        if($isUrl <> false) {
            return true;
        } else {
            return false;
        }
    }
}